package continuity;

/**
 * Zetor csapat szekelton
 * 
 * A program célja hogy a modellről eldönthető legyen, hogy a feladat egy helyes
 * megoldását nyújtja.
 * 
 * @author Balogh Peter Tamas
 * @author Gál István László
 * @author Kárász Gábor
 * @author Simon Endre András
 * @author Szabó Alex
 */
public class Character implements Sprite {

    private Point3d position;
    private boolean hasKey;
    private boolean canMove;
    private Game game;
    private Level level;
    private int moveDir;
    private int moveSpeed;
    private boolean moveNext;
    private boolean gravity;
    /**
     * 
     */
    static public String className = "Character";
    /**
     * 
     */
    public String name;

    /**
     * 
     * @param pos
     * @param g
     * @param n
     */
    public Character(Point3d pos, Game g, String n) {
        name = n;
        game=g;
        position=pos;
        moveSpeed = 1;
        moveNext = false;
        gravity = true;
    }

    /**
     * 
     */
    public void reset() {
    }

    /**
     * 
     * @param pos
     */
    public void setPosition(Point3d pos) {
    }

    public void setMoveNext(boolean b) {
        moveNext=true;
    }
    /**
     * 
     */
    public boolean getHasKey() {
        return hasKey;
    }

    /**
     * 
     */
    public void setHasKey() {
        hasKey=true;
    }

    /**
     * 
     */
    public void resetHasKey() {
        hasKey=false;
                
    }
    
    public int getMoveDir() {
        return moveDir;
    }

    /**
     * 
     * @param dir
     */
    public void move(int dir) {
        if(!game.getActualLevel().getActiveItem().intersectAll(this, dir)) {
              
                Proto.output(
                getName(),
                getClass().toString(),
                "moved(dir="+dir+")", 
                game.getActualLevel().getActiveItem().getName(),
                game.getActualLevel().getActiveItem().getClass().toString());
                
                
                if( dir == 4) {
                    position.setY(position.getY()-moveSpeed);
                    
                } else if( dir == 3) {
                    position.setY(position.getY()+moveSpeed);
                    gravity = false;
                } else if( dir == 2) {
                    position.setX(position.getX()+moveSpeed);
                } else if( dir == 1) {
                    position.setX(position.getX()-moveSpeed);
                }
        } 
    }
    

    /**
     * 
     * @param t
     */
    public void setCanMove(Boolean t) {
        String classInfo = name + "(" + className + ")::setCanMove(Bool t): void";
    }

    /**
     * 
     */
    public void Tick() {
            gravity = true;
            if(moveNext) {
                move(moveDir);
                moveNext = false;
            }
            if( gravity ) move(4);
    }

    /**
     * 
     */
    @Override
    public Point3d getPosition() {
        return position;
    }

    /**
     * 
     * @param s
     */
    @Override
    public boolean checkHit(Sprite s, int dir) {
        //sajat magunkkal utkozest nem vizsgalunk
        if(s == this) return false;
        //s.checkHit(this);
        int x1 = s.getPosition().getX();
        int y1 = s.getPosition().getY();
        int x2 = x1 + s.getWidth();
        int y2 = y1 + s.getHeight();
        int ymax = y1 > y2 ? y1 : y2;
        int ymin = y1 > y2 ? y2 : y1;

        int xmax = x1 > x2 ? x1 : x2;
        int xmin = x1 > x2 ? x2 : x1;
                     
        int tx1 = position.getX();
        int tx2 = tx1+getWidth();
        int ty1 = position.getY();
        int ty2 = ty1+getHeight();
        int tymax = ty1 > ty2 ? ty1 : ty2;
        int tymin = ty1 > ty2 ? ty2 : ty1;
        int txmax = tx1 > tx2 ? tx1 : tx2;
        int txmin = tx1 > tx2 ? tx2 : tx1;
        
        if( dir == 4) {
            tymin-=moveSpeed;
            tymax-=moveSpeed;
        } else if( dir == 3) {
            tymin+=moveSpeed;
            tymax+=moveSpeed;
        } else if( dir == 2) {
            txmin+=moveSpeed;
            txmax+=moveSpeed;
        } else if( dir == 1) {
            txmin-=moveSpeed;
            txmax-=moveSpeed;
        }
        
        if( (ymin-tymax)*(ymax-tymin) <= 0 ) {
            
            Proto.output(
                getName(),
                getClass().toString(),
                "collision", 
                s.getName(),
                s.getClass().toString());
            return true;
        }
        return false;
    }

    /**
     * 
     */
    @Override
    public int getWidth() {
        return 1;
    }

    /**
     * 
     */
    @Override
    public int getHeight() {
        return 1;
    }

    /**
     * 
     * @return
     */
    public String getType() {
        return className;
    }

    /**
     * 
     * @return
     */
    @Override
    public String getName() {
        return name;
    }
    public void setName(String n) {
        name =n;
    }

    public void setLevel(Level l) {
        level = l;
    }
    public void setDir(int dir) {
        moveDir = dir;
    }
    public void setSpeed(int speed) {
        moveSpeed = speed;
    }
}
